﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using DAO;
using System.Xml;
using System.IO;

public partial class TaoGianHang : System.Web.UI.Page
{
    public bool KiemTraGianHang(ThanhVienDTO thanhvien)
    {
        if (GianHangDAO.LayMaGianHangTheoChuGianHang(thanhvien.ID) != 0)
            return true;
        else
            return false;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            if (Session["UserOnline"] == null)
                Response.Redirect("~/Default.aspx");
            else
            {
                ThanhVienDTO thanhvien = new ThanhVienDTO();
                thanhvien = ThanhVienBUS.LayThanhVienTheoUserName(Session["UserOnline"].ToString());
                if (thanhvien.MaNhomThanhVien == 3 || thanhvien.MaNhomThanhVien == 2)
                {
                    if (KiemTraGianHang(thanhvien) == true)
                    {
                        string path = ResolveClientUrl("~/KetQuaTaoGianHang.aspx");
                        Response.Redirect(path + "?Ten=false");
                    }
                    rbtLogo1.Checked = true;

                    List<SanPhamDTO> listSP = SanPhamBUS.LayDanhSachSanPham();

                    CollectionPager2.PageSize =30;
                    CollectionPager2.DataSource = listSP;
                    CollectionPager2.BindToControl = dtlDanhSachSanPham;
                    dtlDanhSachSanPham.DataSource = CollectionPager2.DataSourcePaged;
                    bttHoanTat.Attributes.Add("OnClick", "return HoanTat()'");
                }
                else
                    Response.Redirect("~/Default.aspx");
            }
        }
    }


  

    private string TaoBang(string tensp, string gia)
    {
        string t = "<table width='100%' border='4' cellpadding='4' cellspacing='0' style='background-color:White; '>" +
                "<tr>" +
                    "<td style='width:60%; text-align:left; font-weight:bold;' >" + tensp +
                    "</td>" +
                    "<td style='width:40%; text-align:left; font-weight:bold; color:Red;'>" + gia +
                    "</td>" +
                "</tr>" +
                "</table>";
        return t;
    }

    protected void dtlDanhSachSanPham_ItemCommand(object source, DataListCommandEventArgs e)
    {
        List<SanPhamDTO> listSP = SanPhamBUS.LayDanhSachSanPham();

        CollectionPager2.PageSize = 30;
        CollectionPager2.DataSource = listSP;
        CollectionPager2.BindToControl = dtlDanhSachSanPham;
        dtlDanhSachSanPham.DataSource = CollectionPager2.DataSourcePaged;

        if (e.CommandName == "abc")
        {
            Label tensanpham = (Label)dtlDanhSachSanPham.Items[e.Item.ItemIndex].FindControl("lbTenSanPham");
            Label giaban = (Label)dtlDanhSachSanPham.Items[e.Item.ItemIndex].FindControl("lbGiaBanHienTai");
            //dtlDanhSachSanPham.Items[e.Item.ItemIndex].FindControl("bttAdd").EnableTheming = false;
            int soluong = DemSoLuongSanPhamTrongGianHang();
            if (soluong > 20)
                return;
            else
            {
                if (soluong == 0)
                {
                    lbBangSanPham.Text = TaoBang(tensanpham.Text, giaban.Text);
                }
                else
                {
                    ThemSanPham(tensanpham.Text, giaban.Text, soluong);
                }
                soluong++;
                lbSoLuongSanPham.Text = soluong.ToString();
                lbXuongDong.Text = lbXuongDong.Text + "<br />";
            }
        }
    }

    private void ThemSanPham(string tensp, string gia, int soluong)
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(lbBangSanPham.Text);
        XmlElement tr = xmldoc.CreateElement("tr");
        XmlElement tdTen = xmldoc.CreateElement("td");
        XmlElement tdGia = xmldoc.CreateElement("td");
        XmlAttribute style1 = xmldoc.CreateAttribute("style");
        XmlAttribute style2 = xmldoc.CreateAttribute("style");

        style1.Value = "width:60%; text-align:left; font-weight:bold;";
        tdTen.Attributes.Append(style1);
        style2.Value = "width:40%; text-align:left; font-weight:bold; color:Red;";
        tdGia.Attributes.Append(style2);
        tdTen.InnerXml = tensp;
        tdGia.InnerXml = gia;
        tr.AppendChild(tdTen);
        tr.AppendChild(tdGia);

        XmlElement temp = (XmlElement)xmldoc.DocumentElement.ChildNodes[soluong - 1];
        xmldoc.DocumentElement.InsertAfter(tr, temp);

        StringWriter sw = new StringWriter();
        XmlTextWriter tw = new XmlTextWriter(sw);
        xmldoc.WriteTo(tw);
        lbBangSanPham.Text = sw.ToString();
    }

    
    private int DemSoLuongSanPhamTrongGianHang()
    {
        return int.Parse(lbSoLuongSanPham.Text);
    }

   

    protected void bttHoanTat_Click(object sender, ImageClickEventArgs e)
    {
        if (txtTenGianHang.Text == "" || txtDiaChi.Text == "" ||
            txtSoDienThoai.Text == "" || DemSoLuongSanPhamTrongGianHang()==0)
        {
            lbChuaDienTen.Visible = true;
            lbChuaDienDiaChi.Visible = true;
            lbChuaDienSDT.Visible = true;
            
            return;
        }
        else
        {
            bool temp1 = ThemGianHang();
            bool temp2 = ThemSanPhamThuongDangBanTrenGianHang();
            if (temp1 == true && temp2 == true)
            {
                string path = ResolveClientUrl("~/KetQuaTaoGianHang.aspx");
                Response.Redirect(path + "?Ten=" + txtTenGianHang.Text);
            }
            else
            {
                string path = ResolveClientUrl("~/KetQuaTaoGianHang.aspx");
                Response.Redirect(path + "?Ten=false");
            }
        }
    }

    private string XacDinhLoGo()
    {
        if (rbtLogo1.Checked == true)
            return img1.ImageUrl;
        if (rbtLogo2.Checked == true)
            return img2.ImageUrl;
        if (rbtLogo3.Checked == true)
            return img3.ImageUrl;
        return img4.ImageUrl;
    }


    private bool ThemGianHang()
    {
        ThanhVienDTO thanhvien = ThanhVienBUS.LayThanhVienTheoUserName(Session["UserOnline"].ToString());
        GianHangDTO gianhang = new GianHangDTO(txtTenGianHang.Text,thanhvien.ID,true,XacDinhLoGo(),0,DateTime.Now,DemSoLuongSanPhamTrongGianHang(),0,txtDiaChi.Text,txtSoDienThoai.Text);        
        return GianHangBUS.ThemGianHang(gianhang);
        
    }

    private bool ThemSanPhamThuongDangBanTrenGianHang()
    {
        bool result = false;
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(lbBangSanPham.Text);
        XmlNodeList xmlNL = xmlDoc.SelectNodes("//tr");
        int magianhang = GianHangBUS.LayIDGianHangCuoiCung();

        for (int i = 0; i < xmlNL.Count; i++)
        {
            SanPhamDTO sanpham = SanPhamBUS.LaySanPhamTheoTenSanPham1(xmlNL[i].SelectSingleNode("./td[1]").InnerXml);
            SanPhamThuongDangBanTrenGianHangDTO item = new SanPhamThuongDangBanTrenGianHangDTO(magianhang, sanpham.ID,
                sanpham.GiaBanHienTai, true);
            result = SanPhamThuongDangBanTrenGianHangBUS.ThemSanPhamThuong(item);
        }
        return result;
    }

    private void MessageBox(string msg)
    {
        Label lbl = new Label();
        lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
        Page.Controls.Add(lbl);
    }
}
